package com.data.biz.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertySupport {
    private static final String CONFIG_FILE = "application.properties";
    protected static Log logger = LogFactory.getLog(PropertySupport.class);
    protected static Properties p = new Properties();

    static {
        init("application.properties");
    }

    public PropertySupport() {
    }

    protected static void init(String propertyFileName) {
        InputStream in = null;

        try {
            in = PropertySupport.class.getClassLoader().getResourceAsStream(propertyFileName);
            if (in != null) {
                p.load(in);
                if (logger.isInfoEnabled()) {
                    logger.info("系统配置文件装载成功~");
                }
            }
        } catch (IOException var11) {
            logger.error("装载 [" + propertyFileName + "] 异常:" + var11.getMessage(), var11);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException var10) {
                    in = null;
                    logger.error("释放 [" + propertyFileName + "] 异常:" + var10.getMessage(), var10);
                }
            }

        }

    }

    public static String getProperty(String key, String defaultValue) {
        return StringUtils.defaultIfEmpty(p.getProperty(key), defaultValue);
    }

    public static String getProperty(String key) {
        return p.getProperty(key);
    }

    public static void reload() {
        reload("application.properties");
    }

    public static void reload(String filename) {
        if (p != null) {
            p.clear();
        }

        init(filename);
    }
}
